package com.example.hospital.controller;

import com.example.hospital.common.ApiResponse;
import com.example.hospital.common.PageResponse;
import com.example.hospital.domain.dto.LoginRequest;
import com.example.hospital.domain.entity.*;
import com.example.hospital.service.AdminService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/api/admins")
public class AdminController {
    private final AdminService adminService;
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    @PostMapping("/login")
    public ApiResponse<Admin> login(@Valid @RequestBody LoginRequest req) {
        return ApiResponse.success(adminService.login(req.getUsername(), req.getPassword()));
    }

    /**
     * 更新管理员个人信息
     * @param adminId
     * @param admin
     * @return
     */
    @PutMapping("/{adminId}")
    public ApiResponse<Boolean> updateProfile(@PathVariable("adminId") Long adminId, @RequestBody Admin admin) { admin.setAdminId(adminId); return ApiResponse.success(adminService.updateAdminProfile(admin)); }

    /**
     * 创建医生
     * @param d
     * @return
     */
    @PostMapping("/doctors")
    public ApiResponse<Doctor> createDoctor(@RequestBody Doctor d) {
        return ApiResponse.success(adminService.createDoctor(d));
    }

    /**
     * 创建部门
     * @param d
     * @return
     */
    @PostMapping("/departments")
    public ApiResponse<Department> createDepartment(@RequestBody Department d) {
        return ApiResponse.success(adminService.createDepartment(d));
    }

    /**
     * 更新部门信息
     * @param id
     * @param d
     * @return
     */
    @PutMapping("/departments/{id}")
    public ApiResponse<Boolean> updateDepartment(@PathVariable("id") Long id, @RequestBody Department d) {
        d.setDepartmentId(id);
        return ApiResponse.success(adminService.updateDepartment(d));
    }

     /**
      * 删除部门
      * @param id
      * @return
      */
    @DeleteMapping("/departments/{id}")
    public ApiResponse<Boolean> deleteDepartment(@PathVariable("id") Long id) {
        return ApiResponse.success(adminService.deleteDepartment(id));
    }

     /**
      * 列出所有部门
      * @param name
      * @param page
      * @param size
      * @return
      */
    @GetMapping("/departments")
    public ApiResponse<PageResponse<Department>> listDepartments(@RequestParam(name = "name", required = false) String name, @RequestParam(name = "page", defaultValue = "1") int page, @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(adminService.listDepartments(name, page, size));
    }



    /**
     * 创建职务
     * @param p
     * @return
     */
    @PostMapping("/positions")
    public ApiResponse<Position> createPosition(@RequestBody Position p) {
        return ApiResponse.success(adminService.createPosition(p));
    }

     /**
      * 更新职务信息
      * @param id
      * @param p
      * @return
      */
    @PutMapping("/positions/{id}")
    public ApiResponse<Boolean> updatePosition(@PathVariable("id") Long id, @RequestBody Position p) {
        p.setPositionId(id); return ApiResponse.success(adminService.updatePosition(p));
    }

     /**
      * 删除职务
      * @param id
      * @return
      */
    @DeleteMapping("/positions/{id}")
    public ApiResponse<Boolean> deletePosition(@PathVariable("id") Long id) {
        return ApiResponse.success(adminService.deletePosition(id));
    }

     /**
      * 列出所有职务
      * @param name
      * @param page
      * @param size
      * @return
      */
    @GetMapping("/positions")
    public ApiResponse<PageResponse<Position>> listPositions(@RequestParam(name = "name", required = false) String name,
                                                             @RequestParam(name = "page", defaultValue = "1") int page,
                                                             @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(adminService.listPositions(name, page, size));
    }


    /**
     * 创建指标分类
     * @param c
     * @return
     */
    @PostMapping("/indicator-categories")
    public ApiResponse<IndicatorCategory> createCategory(@RequestBody IndicatorCategory c) {
        return ApiResponse.success(adminService.createCategory(c));
    }
     /**
      * 更新指标分类信息
      * @param id
      * @param c
      * @return
      */
    @PutMapping("/indicator-categories/{id}")
    public ApiResponse<Boolean> updateCategory(@PathVariable("id") Long id,
                                               @RequestBody IndicatorCategory c) {
        c.setCategoryId(id); return ApiResponse.success(adminService.updateCategory(c));
    }
     /**
      * 删除指标分类
      * @param id
      * @return
      */
    @DeleteMapping("/indicator-categories/{id}")
    public ApiResponse<Boolean> deleteCategory(@PathVariable("id") Long id) {
        return ApiResponse.success(adminService.deleteCategory(id));
    }

     /**
      * 列出所有指标分类
      * @param name
      * @param page
      * @param size
      * @return
      */
    @GetMapping("/indicator-categories")
    public ApiResponse<PageResponse<IndicatorCategory>> listCategories(@RequestParam(name = "name", required = false) String name, @RequestParam(name = "page", defaultValue = "1") int page, @RequestParam(name = "size", defaultValue = "10") int size) { return ApiResponse.success(adminService.listCategories(name, page, size)); }

    /**
     * 创建指标
     * @param i
     * @return
     */
    @PostMapping("/indicators")
    public ApiResponse<ExaminationIndicator> createIndicator(@RequestBody ExaminationIndicator i) {
        return ApiResponse.success(adminService.createIndicator(i));
    }
     /**
      * 更新指标信息
      * @param id
      * @param i
      * @return
      */
    @PutMapping("/indicators/{id}")
    public ApiResponse<Boolean> updateIndicator(@PathVariable("id") Long id,
                                                @RequestBody ExaminationIndicator i) {
        i.setIndicatorId(id);
        return ApiResponse.success(adminService.updateIndicator(i));

    }


     /**
      * 删除指标
      * @param id
      * @return
      */
    @DeleteMapping("/indicators/{id}")
    public ApiResponse<Boolean> deleteIndicator(@PathVariable("id") Long id) {
        return ApiResponse.success(adminService.deleteIndicator(id));
    }


    /**
     * 列出所有指标
     * @param code
     * @param name
     * @param categoryId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/indicators")
    public ApiResponse<PageResponse<ExaminationIndicator>> listIndicators(@RequestParam(name = "code",
            required = false) String code, @RequestParam(name = "name", required = false) String name,
                                                                          @RequestParam(name = "categoryId",required = false) Long categoryId,
                                                                          @RequestParam(name = "page", defaultValue = "1") int page,
                                                                          @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(adminService.listIndicators(code, name, categoryId, page, size));
    }

    /**
     * 创建套餐
     * @param p
     * @return
     */
    @PostMapping("/packages")
    public ApiResponse<ExamPackage> createPackage(@RequestBody ExamPackage p) {
        return ApiResponse.success(adminService.createPackage(p));
    }

     /**
      * 更新套餐信息
      * @param id
      * @param p
      * @return
      */
    @PutMapping("/packages/{id}")
    public ApiResponse<Boolean> updatePackage(@PathVariable("id") Long id, @RequestBody ExamPackage p) {
        p.setPackageId(id); return ApiResponse.success(adminService.updatePackage(p));
    }

     /**
      * 删除套餐
      * @param id
      * @return
      */
    @DeleteMapping("/packages/{id}")
    public ApiResponse<Boolean> deletePackage(@PathVariable("id") Long id) {
        return ApiResponse.success(adminService.deletePackage(id));
    }

     /**
      * 列出所有套餐
      * @param name
      * @param page
      * @param size
      * @return
      */
    @GetMapping("/packages")
    public ApiResponse<PageResponse<ExamPackage>> listPackages(@RequestParam(name = "name", required = false) String name, @RequestParam(name = "page", defaultValue = "1") int page, @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(adminService.listPackages(name, page, size));
    }

     /**
      * 添加指标到套餐
      * @param id
      * @param indicatorId
      * @return
      */
    @PostMapping("/packages/{id}/items")
    public ApiResponse<PackageItem> addPackageItem(@PathVariable("id") Long id,
                                                   @RequestParam(name = "indicatorId") Long indicatorId) {
        PackageItem item = new PackageItem(); item.setPackageId(id); item.setIndicatorId(indicatorId);
        return ApiResponse.success(adminService.addPackageItem(item));
    }

     /**
      * 从套餐中删除指标
      * @param id
      * @return
      */
    @DeleteMapping("/packages/items/{id}")
    public ApiResponse<Boolean> removePackageItem(@PathVariable("id") Long id) {
        return ApiResponse.success(adminService.removePackageItem(id));
    }

     /**
      * 列出套餐中的指标
      * @param id
      * @return
      */
    @GetMapping("/packages/{id}/items")
    public ApiResponse<java.util.List<PackageItem>> listPackageItems(@PathVariable("id") Long id) {
        return ApiResponse.success(adminService.listPackageItems(id));
    }

     /**
      * 列出所有用户（包括医生、管理员）
      * @param name
      * @param phone
      * @param gender
      * @param page
      * @param size
      * @return
      */
    @GetMapping("/users")
    public ApiResponse<PageResponse<User>> listUsers(@RequestParam(name = "name", required = false) String name,
                                                     @RequestParam(name = "phone", required = false) String phone,
                                                     @RequestParam(name = "gender", required = false) String gender,
                                                     @RequestParam(name = "page", defaultValue = "1") int page,
                                                     @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(adminService.listUsers(name, phone, gender, page, size));
    }

     /**
      * 列出所有医生
      * @param name
      * @param departmentId
      * @param positionId
      * @param page
      * @param size
      * @return
      */
    @GetMapping("/doctors")
    public ApiResponse<PageResponse<Doctor>> listDoctors(@RequestParam(name = "name", required = false) String name,
                                                          @RequestParam(name = "departmentId", required = false) Long departmentId,
                                                          @RequestParam(name = "positionId", required = false) Long positionId,
                                                          @RequestParam(name = "page", defaultValue = "1") int page,
                                                          @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(adminService.listDoctors(name, departmentId, positionId, page, size));
    }

     /**
      * 列出所有管理员
      * @param name
      * @param page
      * @param size
      * @return
      */
    @GetMapping("/admins")
    public ApiResponse<PageResponse<Admin>> listAdmins(@RequestParam(name = "name", required = false) String name,
                                                       @RequestParam(name = "page", defaultValue = "1") int page,
                                                       @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(adminService.listAdmins(name, page, size));
    }

     /**
      * 列出所有订单
      * @param userId
      * @param doctorId
      * @param status
      * @param orderType
      * @param page
      * @param size
      * @return
      */
    @GetMapping("/orders")
    public ApiResponse<PageResponse<ExaminationOrder>> listOrders(@RequestParam(name = "userId", required = false) Long userId,
                                                                  @RequestParam(name = "doctorId", required = false) Long doctorId,
                                                                  @RequestParam(name = "status", required = false) Integer status,
                                                                  @RequestParam(name = "orderType", required = false) String orderType,
                                                                   @RequestParam(name = "page", defaultValue = "1") int page,
                                                                   @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(adminService.listOrders(userId, doctorId, status, orderType, page, size));
    }

     /**
      * 创建医生排班
      * @param s
      * @return
      */
    @PostMapping("/schedules")
    public ApiResponse<DoctorSchedule> createSchedule(@RequestBody DoctorSchedule s) {
        return ApiResponse.success(adminService.createSchedule(s));
    }

     /**
      * 更新医生排班
      * @param id
      * @param s
      * @return
      */
    @PutMapping("/schedules/{id}")
    public ApiResponse<Boolean> updateSchedule(@PathVariable("id") Long id, @RequestBody DoctorSchedule s) {
        s.setScheduleId(id);
        return ApiResponse.success(adminService.updateSchedule(s));
    }

     /**
      * 删除医生排班
      * @param id
      * @return
      */
    @DeleteMapping("/schedules/{id}")
    public ApiResponse<Boolean> deleteSchedule(@PathVariable("id") Long id) {
        return ApiResponse.success(adminService.deleteSchedule(id));
    }

     /**
      * 列出所有医生排班
      * @param doctorId
      * @param date
      * @param page
      * @param size
      * @return
      */
    @GetMapping("/schedules")
    public ApiResponse<PageResponse<DoctorSchedule>> listSchedules(@RequestParam(name = "doctorId", required = false) Long doctorId,
                                                                   @RequestParam(name = "date", required = false) String date,
                                                                   @RequestParam(name = "page", defaultValue = "1") int page,
                                                                   @RequestParam(name = "size", defaultValue = "10") int size) {
        LocalDate d = (date == null || date.isEmpty()) ? null : LocalDate.parse(date);
        return ApiResponse.success(adminService.listSchedules(doctorId, d, page, size));
    }
}